<template>
  <div class="auxiliary">
    <page-header title="一级分类列表">
      <page-search :config="searchConfig" :model="searchModel" @search="getData">
        <el-button @click="showDialog()">添加一级分类</el-button>
      </page-search>
    </page-header>
    <el-table :data="tableData" :cell-class-name="setCellClassName" :max-height="clientHeight - 250">
      <el-table-column prop="name" label="一级分类名称" min-width="120px"/>
      <el-table-column prop="iconUrl" label="图标" min-width="120px">
        <el-avatar :size="42" :src="row.iconUrl" slot-scope="{ row }"></el-avatar>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" min-width="200px" />
      <el-table-column label="操作" min-width="80px">
        <template slot-scope="{ row }">
          <action-text @click="showDialog(row)">修改</action-text>
          <action-text @click="deleteData(row)" type="danger">删除</action-text>
        </template>
      </el-table-column>
    </el-table>
    <page-pagination
      :pageSize="pageSize"
      :currentPage="pageNum"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <formDialog
      ref="dialog"
      :initFormData="formData"
      :isCreate="isCreate"
      @confirm="getData"
    />
  </div>
</template>
<script>
import paginationMixin from '@/mixins/pagination';
import { getCategoryList, deleteCategory } from '@/api/course';
import formDialog from './dialog';

const initFormData = {
  name: '',
  iconUrl: '',
  level: 1,
};
export default {
  data() {
    return {
      searchConfig: [
        {
          type: 'input',
          label: '一级分类名称',
          prop: 'name',
        },
      ],
      searchModel: {
        name: '',
      },
      tableData: [],
      formData: { ...initFormData },
      isCreate: false,
    };
  },
  components: {
    formDialog,
  },
  mixins: [paginationMixin],
  methods: {
    viewDetail(row) {
      console.log(row);
    },
    setCellClassName({ column }) {
      if (column.property === 'iconUrl') {
        return 'image-cell';
      }
      return '';
    },
    async deleteData(row) {
      this.$confirm('确定删除', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        await deleteCategory({
          courseCategoryId: row.id,
        });
        this.$message.success('删除成功');
        await this.getData();
      }).catch(() => {});
    },
    showDialog(data) {
      if (data) {
        const {
          id, iconUrl, name, level,
        } = data;
        this.formData = {
          id, iconUrl, name, level,
        };
        this.isCreate = false;
      } else {
        this.formData = { ...initFormData };
        this.isCreate = true;
      }
      this.$refs.dialog.dialogVisible = true;
    },
    async getData() {
      const result = await getCategoryList({
        ...this.searchParams,
        level: 1,
      });
      const { total, list } = result;
      this.total = total;
      this.tableData = list && list.map((item) => {
        const { createTime } = item;
        return {
          ...item,
          createTime: this.$formatTime(createTime),
        };
      });
    },
  },
  async created() {
    await this.getData();
  },
};
</script>
